/* -+- file  ./kf2/Core/GnomeConfiguration.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;

namespace Habazie.kf2.Core
{
	///<summary>GConf configuration database backend</summary>
	public class GnomeConfiguration : GConf.Client, IConfiguration
	{
		static string BASE_PATH = "/apps/talkative/";
		
		public GnomeConfiguration() : base ()
		{
		}
		
		///<summary>Load configuration</summary>
		public void Load ()
		{
		}
		
		///<summary>Store configuration</summary>
		public void Store ()
		{
			SuggestSync ();
		}
		
		///<summary>Get integer value</summary>
		///<param name="key">Key name</param>
		public int GetInt (string key)
		{
			try {
				return (int) Get (BASE_PATH + key);
			} catch (GConf.NoSuchKeyException) {
				Util.Debug ("conf", "No such key: " + key);
				return 0;
			} catch (System.InvalidCastException) {
				Util.Debug ("conf", "Cannot typecast key " + key);
				return 0;
			}
		}
		
		///<summary>Get boolean value</summary>
		///<param name="key">Key name</param>
		public bool GetBool (string key)
		{
			try {
				return (bool) Get (BASE_PATH + key);
			} catch (GConf.NoSuchKeyException) {
				Util.Debug ("conf", "No such key: " + key);
				return false;
			} catch (System.InvalidCastException) {
				Util.Debug ("conf", "Cannot typecast key " + key);
				return false;
			}
		}
		
		
		///<summary>Get integer value</summary>
		///<param name="key">Key name</param>
		public string GetString (string key)
		{
			try {
				return (string) Get (BASE_PATH + key);
			} catch (GConf.NoSuchKeyException) {
				Util.Debug ("conf", "No such key: " + key);
				return null;
			} catch (System.InvalidCastException) {
				Util.Debug ("conf", "Cannot typecast key " + key);
				return null;
			}
		}
		
		///<summary>Set integer value</summary>
		///<param name="key">Key name</param>
		///<param name="val">New key value to be set</param>
		public void SetInt (string key, int val)
		{
			try {
				Set (BASE_PATH + key, val);
			} catch (Exception) {
				Util.Debug ("conf", "Exception: " + key);
			}
		}
		
		///<summary>Set boolean value</summary>
		///<param name="key">Key name</param>
		///<param name="val">New key value to be set</param>
		public void SetBool (string key, bool val)
		{
			try {
				Set (BASE_PATH + key, val);
			} catch (Exception) {
				Util.Debug ("conf", "Exception: " + key);
			}
		}
		
		///<summary>Set integer value</summary>
		///<param name="key">Key name</param>
		///<param name="val">New key value to be set</param>
		public void SetString (string key, string val)
		{
			try {
				Set (BASE_PATH + key, val);
			} catch (Exception) {
				Util.Debug ("conf", "Exception: " + key);
			}
		}
		
		
		public bool KeyIsWritable (string key)
		{
			return true;
		}
		
		
		public void AddNotify (string key, ConfigNotifyEventHandler notify)
		{
			Util.Debug ("Adding notify for {0}", BASE_PATH + key);
			base.AddNotify (BASE_PATH + key, delegate (object sender, GConf.NotifyEventArgs args) {
				notify (this, new ConfigNotifyEventArgs (args.Key, args.Value));
			});
		}
	}
	
}
